﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FreeDream.Common.Helper
{
    public class PrintTempHelper
    {
        public static string QueryPrintFile(string tempid, dynamic data, string businessType = "SFZHZ")
        {
            try
            {
                string htmltemppaht = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "PrintTemp", $"{tempid}.html");
                if (!FileHelper.IsExistFile(htmltemppaht))
                {
                    //LogInfo("QueryPrintFile", "模板未查询到，请确认模板是否正确！！", _logger);
                    return "";
                }
                var result = FileHelper.FileToString(htmltemppaht, System.Text.Encoding.UTF8);
                if (string.IsNullOrWhiteSpace(result))
                {
                    //LogInfo("QueryPrintFile", "模板为空，请确认模板是否正确！", _logger);
                    return "";
                }
                #region html 模板替换
                string relativePath = Path.Combine("upload", businessType, $"{DateTime.Now:yyyyMMdd}");
                string saveRes = $"{tempid}记录保存成功";

                // 通用模板,只需要传入时间其他的不需要
                var outprintfilename = tempid + "_" + DateTime.Now.GetTimeStamp13();
                //data.time = DateTime.Now.ToString("yyyy年MM月dd日");
                var contextDefault = new InstallContext();
                contextDefault.SetDynamicModel(data);
                var stroutpur = JsonExtends.ReplaceStringVar(result, contextDefault, @"\{\{(.*?)\}\}");
                var outputpath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, relativePath);
                #endregion

                FileHelper.WriteFile(stroutpur, Path.Combine(outputpath, $"{outprintfilename}.html"));
                return Path.Combine(relativePath, $"{outprintfilename}.html");
            }
            catch (Exception ex)
            {
                var ss = ex.Message;
                return "";
            }
        }
        public static string QueryPrintHtml(string tempid, dynamic data, string businessType = "SFZHZ")
        {
            try
            {
                string htmltemppaht = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "PrintTemp", $"{tempid}.html");
                if (!FileHelper.IsExistFile(htmltemppaht))
                {
                    //LogInfo("QueryPrintFile", "模板未查询到，请确认模板是否正确！！", _logger);
                    return "";
                }
                var result = FileHelper.FileToString(htmltemppaht, System.Text.Encoding.UTF8);
                if (string.IsNullOrWhiteSpace(result))
                {
                    //LogInfo("QueryPrintFile", "模板为空，请确认模板是否正确！", _logger);
                    return "";
                }
                #region html 模板替换
                // 通用模板,只需要传入时间其他的不需要
                var contextDefault = new InstallContext();
                contextDefault.SetDynamicModel(data);
                return JsonExtends.ReplaceStringVar(result, contextDefault, @"\{\{(.*?)\}\}");
                #endregion 
            }
            catch (Exception ex)
            {
                var ss = ex.Message;
                return "";
            }
        }
    }
}
